package com.gs.synedocket.fragment;

import android.view.View;
import android.widget.Button;

import com.gs.synedocket.R;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.control.QRCodeView;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.db.dto.DriverResetDto;
import com.gs.synedocket.db.sqlite.DBApi;
import com.gs.synedocket.dialog.DialogUtil;
import com.gs.synedocket.dialog.LoadingDialogUtils;
import com.gs.synedocket.dialog.SureDialog;
import com.gs.synedocket.dialog.ToastDialogUtils;
import com.gs.synedocket.fragment.base.BaseFragment;
import com.gs.synedocket.presenter.QRCodePresenter;
import com.gs.synedocket.util.AppUtil;
import com.gs.synedocket.util.DocketUtil;
import com.gs.synedocket.util.ToastUtil;
import com.gs.synedocket.view.IQRCodeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class QRCodeFragment extends BaseFragment<IQRCodeView, QRCodePresenter> implements IQRCodeView {

    private QRCodeView viewAuthority, viewCustomer, viewKWah;
    private SureDialog sureDialog;
    private Button btnReset;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_qr_code;
    }

    @Override
    protected QRCodePresenter createPresenter() {
        return new QRCodePresenter();
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        viewAuthority = fd(R.id.view_authority);
        viewCustomer = fd(R.id.view_customer);
        viewKWah = fd(R.id.view_k_wah);
        btnReset = fd(R.id.btn_reset);
        if (DocketUtil.getInstance().isLogDetail()) {
            btnReset.setVisibility(View.GONE);
        }
        btnReset.setOnClickListener(v -> {
            if (DocketUtil.getInstance().getUpgradeLevel() == AllConstants.UPGRADE_FIRST) {
                ToastUtil.showToast(R.string.upgrade_first);
                LogUtil.getInstance().insertOrReplaceData(getString(R.string.upgrade_log, AppUtil.getInstance().getVersion()));
                return;
            }
            if (DocketUtil.getInstance().getDocketId().isEmpty()) {
                ToastUtil.showToast(R.string.empty_docket);
                return;
            }
            Integer truckState = DocketUtil.getInstance().getTruckState();
            if (truckState >= 4 || truckState == 0) {
                ToastUtil.showToast(R.string.not_reset);
                return;
            }
            if (truckState == 1 && DocketUtil.getInstance().getQcState() == -1) {
                ToastUtil.showToast(R.string.no_reset);
                return;
            }
            if (sureDialog == null) {
                sureDialog = new SureDialog(context, "", () -> {
                    DriverResetDto bean = new DriverResetDto();
                    bean.setDocketId(DocketUtil.getInstance().getDocketId());
                    bean.setTruckNo(DocketUtil.getInstance().getTruckNo());
                    mPresenter.driverResetSign(bean);
                });
            }
            sureDialog.show();
        });
    }

    @Override
    protected void initData() {
        viewAuthority.setImageContent(DocketUtil.getInstance().getQrCodeAuth());
        viewCustomer.setImageContent(DocketUtil.getInstance().getQrCodeCus());
        viewKWah.setImageContent(DocketUtil.getInstance().getQrCodeKWah());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!DocketUtil.getInstance().isLogDetail()) {
                if (btnReset.getVisibility() == View.GONE) {
                    btnReset.setVisibility(View.VISIBLE);
                }
            }
            initData();
        }
    }

    @Override
    public void onRevertSuccess(ApiResultData<String> apiResult) {
        ToastDialogUtils.getInstance().showInfoMessage(context, apiResult.getMessage());
        if (apiResult.getStatus() != 2000) {
            return;
        }
        LogUtil.getInstance().insertOrReplaceData(DocketUtil.getInstance().getDocketNo() + "-重置狀態成功");
        DocketUtil.getInstance().setTruckState(1);
        DocketUtil.getInstance().setDriverState(1);
        DocketUtil.getInstance().setQcState(-1);
        DocketUtil.getInstance().setAccRep(null);
        DocketUtil.getInstance().setAdditional(null);
        DBApi.getInstance().updateDocketTruckState(DocketUtil.getInstance().getDocketId(), 1);
        EventBus.getDefault().post(new Event(EventCode.EVENT_RESET));
    }

    @Override
    public void onRevertFails(Exception msg) {
        ToastDialogUtils.getInstance().showInfoMessage(context, R.string.network_error);
    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(context);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        int what = event.getWhat();
        switch (what) {
            case EventCode.EVENT_LOG_DETAIL:
                btnReset.setVisibility(View.GONE);
                break;
            case EventCode.EVENT_RETURN_DOCKET:
                btnReset.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DialogUtil.getInstance().dialogDestroy(sureDialog);
    }
}
